package com.qi.controller;

import com.alibaba.fastjson.JSONObject;
import com.qi.commom.Clazz;
import com.qi.commom.Student;
import com.qi.result.Result;
import com.qi.service.ClazzService;
import com.qi.service.StudentService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.util.ArrayList;

@Controller
@RequestMapping("/boot")
@Slf4j
public class controller {

    @Autowired
    private StudentService studentService;

    @Autowired
    private ClazzService clazzService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 查询全部学生
     * @return
     */
    @RequestMapping("/selectStudent")
    public String selectStudent (Model model){
        log.info("接口功能：【{}】，请求地址：【{}】，请求方式：【{}】，请求参数：【无】",
                "查询全部学生",request.getRequestURI(),request.getMethod());

        ArrayList<Student> list = studentService.selectStudent();
        Result<ArrayList<Student>> studentList = Result.success(list);
        model.addAttribute("studentList",studentList);

        log.info("接口功能：【{}】，请求地址：【{}】，请求方式：【{}】，响应结果：【{}】",
                "查询全部学生",request.getRequestURI(),request.getMethod(), JSONObject.toJSONString(list));
        return "stuList";
    }

    /**
     * 查询全部的班级
     * @return
     */
    @RequestMapping("/selectClazz")
    public String selectClazz (Model model){
        log.info("接口功能：【{}】，请求地址：【{}】，请求方式：【{}】，请求参数：【无】",
                "查询全部的班级",request.getRequestURI(),request.getMethod());

        ArrayList<Clazz> list = clazzService.selectClazz();
        Result<ArrayList<Clazz>> clazzList = Result.success(list);
        model.addAttribute("clazzList",clazzList);

//        sendCode.sendMsg("19997098309","5");
//        sendCode.sendMsg("19997098309","2");
//        sendCode.sendMsg("19997098309","1");

        log.info("接口功能：【{}】，请求地址：【{}】，请求方式：【{}】，响应结果：【{}】",
                "查询全部的班级",request.getRequestURI(),request.getMethod(), JSONObject.toJSONString(list));

        return "claList";
    }

    /**
     * 添加学生
     * @param student
     * @return
     */
    @PostMapping("/insertStudent")
    @ResponseBody
    public Integer InsertStudent(@RequestBody Student student){
        log.info("接口功能：【{}】，请求地址：【{}】，请求方式：【{}】，请求参数：【{}】",
                "添加学生",request.getRequestURI(),request.getMethod(),student);

        Integer result = studentService.insertStudent(student);

        log.info("接口功能：【{}】，请求地址：【{}】，请求方式：【{}】，响应结果：【{}】",
                "添加学生",request.getRequestURI(),request.getMethod(), JSONObject.toJSONString(result));

        return result;
    }

    /**
     * 去首页
     * @return
     */
    @RequestMapping("/index.html")
    public String index(){
        return "index";
    }

    /**
     * 查询全部的班级用作下拉框展示
     * @return
     */
    @RequestMapping("/findClazzAll")
    @ResponseBody
    public ArrayList<Clazz> findClazzAll(){
        ArrayList<Clazz> list = clazzService.selectClazz();
        //把查到的班级数据映射到Redis内存服务器中
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        ops.set("clazzList",JSONObject.toJSONString(list));
        redisTemplate.expire("clazzList", Duration.ofMinutes(10));
        return list;
    }
}
